package cn.gson.crm.controller.employee;

import cn.gson.crm.controller.BaseServlet;
import cn.gson.crm.model.dao.DeptDao;
import cn.gson.crm.model.dao.EmpDao;

import cn.gson.crm.model.dao.PositionDao;
import cn.gson.crm.model.entity.Dept;
import cn.gson.crm.model.entity.Position;
import cn.gson.crm.vo.EmployeeVo;

import java.util.List;
import java.util.Map;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 员工管理主界面
 */
@WebServlet("/employee")
public class EmployeeServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;
	private EmpDao empDao = new EmpDao();
	private DeptDao deptDao=new DeptDao();
	private PositionDao postDao=new PositionDao();

	protected void get(HttpServletRequest request, HttpServletResponse response) throws Exception {

		// List<Employee> empList = empDao.getEmpList();
		// 输出emp的全部信息+错误信息
		// 创建错误的错误信息

		String empName = (String) request.getSession().getAttribute("empName");
		if (empName != null) {
			request.getSession().setAttribute("empName", "");
			request.setAttribute("empName", empName);
		}

		String empStatus=request.getParameter("empStatus");
		if(empStatus==null||empStatus.isEmpty()){
			empStatus=1+"";
		}
		//获得所有的部门信息
		List<Dept> deptList=deptDao.getAllDept();
		
		//获得所有的职位信息
		List<Position> postList= postDao.getPostByStatus(1);

		// 查询+分页
		String p = request.getParameter("page");
		String userName = request.getParameter("userName");
		String searchdept = request.getParameter("searchdept");
		String searchpost = request.getParameter("searchpost");
		if (p == null || p.isEmpty()) {
			p = "1";
		}
		
		Integer page = Integer.parseInt(p);
		
		//获取员工状态
		Integer empsta=Integer.parseInt(empStatus);
		
		//获得全部信息System.out.println(sql);
		List<EmployeeVo> empVoList = empDao.getEmpAllList(empsta, page, userName, searchdept, searchpost);
		// 总页数
		int totalPage = empDao.getTotalPage(empsta, userName, searchdept, searchpost);

		request.setAttribute("queryString", noPageQueryString(request));
		request.setAttribute("totalPage", totalPage);
		request.setAttribute("page", page);
		request.setAttribute("empVoList", empVoList);
		request.setAttribute("deptList", deptList);
		request.setAttribute("postList", postList);
		forward("employee/emp_manage.jsp");

	}

	/**
	 * 
	 * @param request
	 * @return
	 */
	private String noPageQueryString(HttpServletRequest request) {
		Map<String, String[]> params = request.getParameterMap();

		String paramsStr = "";
		int i = 0;
		for (String key : params.keySet()) {
			if ("page".equals(key)) {
				continue;
			}
			String[] valus = params.get(key);
			String query = "";
			for (int j = 0; j < valus.length; j++) {
				query += key + "=" + valus[j];
				if (j < valus.length - 1) {
					query += "&";
				}
			}
			paramsStr += query;
			if (i < params.keySet().size() - 1) {
				paramsStr += "&";
			}
			i++;
		}
		return paramsStr.isEmpty() ? "" : "&" + paramsStr;
	}

}
